<?php

namespace App\Http\Controllers;

use App\Http\Requests\BindRequest;
use App\Http\Requests\CardRequest;
use App\Http\Requests\FeedbackRequest;
use App\Http\Requests\InviteListRequest;
use App\Http\Requests\LikeRequest;
use App\Http\Requests\UpdateRequest;
use App\Http\Requests\WithdrawRequest;
use App\Http\Services\UserService;
use App\Models\Card;
use Illuminate\Http\Request;

class UserController extends Controller
{
    protected $service;

    public function __construct(UserService $service)
    {
        $this->service = $service;
    }

    public function like(LikeRequest $request)
    {
        $user = $request->user();
        $key = 'like:' . $user->id;
        if (!redisLock($key)) return response()->json(['code' => 0, 'msg' => '请求频繁']);
        $data = $request->validated();
        $data['uid'] = $user->id;
        $res = $this->service->like($data);
        redisUnlock($key);
        return response()->json($res);
    }

    public function info(Request $request)
    {
        $user = $request->user();
        $user->card = Card::where('uid', $user->id)->first();
        return response()->json(['code' => 200, 'data' => $user]);
    }

    public function upload(Request $request)
    {
        $date = date('Ymd');
        $path = $request->file('image')->store('public/images/'.$date);

        return env('APP_URL') . '/storage'.trim($path, 'public');
    }

    public function feedback(FeedbackRequest $request)
    {
        $user = request()->user();
        $key = 'feedback:' . $user->id;
        if (!redisLock($key)) return response()->json(['code' => 0, 'msg' => '请求频繁']);
        $data = $request->validated();
        $data['uid'] = $user->id;
        $res = $this->service->feedback($data);
        redisUnlock($key);
        return response()->json($res);
    }

    public function update(UpdateRequest $request)
    {
        $user = request()->user();
        $key = 'update:' . $user->id;
        if (!redisLock($key)) return response()->json(['code' => 0, 'msg' => '请求频繁']);
        $data = $request->validated();
        $data['uid'] = $user->id;
        $res = $this->service->update($data);
        redisUnlock($key);
        return response()->json($res);
    }

    public function logoff()
    {
        $user = request()->user();
        $res = $this->service->logoff($user);
        return response()->json($res);
    }

    public function likeList()
    {
        $user = request()->user();
        $res = $this->service->likeList($user);
        return response()->json($res);
    }

    public function inviteList(InviteListRequest $request)
    {
        $user = request()->user();
        $data = request()->all();
        $res = $this->service->inviteList($user, $data);
        return response()->json($res);
    }

    public function inviteCode()
    {
        $user = request()->user();
        $res = $this->service->inviteCode($user);
        return response()->json($res);
    }

    public function card(CardRequest $request)
    {
        $user = request()->user();
        $key = 'card:' . $user->id;
        if (!redisLock($key)) return response()->json(['code' => 0, 'msg' => '请求频繁']);
        $data = $request->validated();
        $data['uid'] = $user->id;
        $res = $this->service->card($data);
        redisUnlock($key);
        return response()->json($res);
    }

    public function withdraw(WithdrawRequest $request)
    {
        $user = request()->user();
        $key = 'withdraw:' . $user->id;
        if (!redisLock($key)) return response()->json(['code' => 0, 'msg' => '请求频繁']);
        $data = $request->validated();
        $res = $this->service->withdraw($data, $user);
        redisUnlock($key);
        return response()->json($res);
    }

    public function withdrawList()
    {
        $user = request()->user();
        $data = request()->all();
        $res = $this->service->withdrawList($data, $user);
        return response()->json($res);
    }

    public function reward()
    {
        $user = request()->user();
        $data = request()->all();
        $res = $this->service->reward($data, $user);
        return response()->json($res);
    }

    public function bind(BindRequest $request)
    {
        $user = request()->user();
        $key = 'bind:' . $user->id;
        if (!redisLock($key)) return response()->json(['code' => 0, 'msg' => '请求频繁']);
        $data = $request->validated();
        $res = $this->service->bind($data, $user);
        redisUnlock($key);
        return response()->json($res);
    }
}
